//
//  expressDataTableViewCell.m
//  A Learning Walk
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "expressDataTableViewCell.h"

static NSString *const Pcell = @"Pcell";

@interface expressDataTableViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, assign)float heightC;

@end

@implementation expressDataTableViewCell

-(void)dealloc{
    
    [_labelT release];
    [_labelZ release];
    [_labelS release];
    [_imageT release];
    [_imageN release];
    
    [_buttonF release];
    [_Ptable release];
    [_Ptext release];
    
    [_model release];
    [_attch release];
    

    
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatCell];
    }
    return self;
}
-(void)creatCell{
    self.imageT = [[UIImageView alloc] init];
    self.imageT.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.imageT];
//作者
    self.labelZ = [FengLabel LabelwithFrame:CGRectMake(0, 0, 0, 0) Color:[UIColor clearColor] font:17];
    [self.contentView addSubview:self.labelZ];
//时间
    self.labelS = [FengLabel LabelwithFrame:CGRectMake(0, 0, 0, 0) Color:[UIColor clearColor] font:17.];
    [self.contentView addSubview:self.labelS];
//分享按钮
#pragma make 分享点击
    self.buttonF = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(0, 0, 0, 0) title:@"" image:[UIImage imageNamed:@""] color:[UIColor greenColor] handler:^(UIButton *sender) {
        
        NSLog(@"分享");
        
    }];
    [self.contentView addSubview:self.buttonF];
//内容
    self.labelT = [FengLabel LabelwithFrame:CGRectMake(0, 0, 0, 0) Color:[UIColor clearColor] font:20];
    [self.contentView addSubview:self.labelT];
    self.labelT.numberOfLines = 0;
//图片
    self.imageN = [[UIImageView alloc] init];
//    self.imageN.backgroundColor = [UIColor blackColor];
    [self.contentView addSubview:self.imageN];
//tableView
    self.Ptable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
//    self.Ptable.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:self.Ptable];
    self.Ptable.delegate = self;
    self.Ptable.dataSource = self;
    self.Ptable.bounces = NO;
    [self.Ptable registerClass:[expressXiaoTableViewCell class] forCellReuseIdentifier:Pcell];
   
    
    
//发表评论
    self.Ptext = [[UITextField alloc] init];
//    self.Ptext.backgroundColor = [UIColor brownColor];
    [self.contentView addSubview:self.Ptext];
    
    [self.imageN release];
    [self.imageT release];
    [self.labelS release];
    [self.labelT release];
    [self.labelZ release];
    
}
-(void)layoutSubviews{
    [super layoutSubviews];
    [self.imageT mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.mas_equalTo(15);
        make.width.height.mas_equalTo(self.contentView.frame.size.width/7);
    }];
    [self.labelZ mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_imageT.mas_right).offset(10);
        make.top.mas_equalTo(_imageT);
        make.width.mas_equalTo(self.contentView.frame.size.width/5);
        make.height.mas_equalTo(25);
    }];
    [self.labelS mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_labelZ.mas_bottom).offset(10);
        make.left.mas_equalTo(_labelZ);
        make.width.mas_equalTo(_labelZ);
        make.height.mas_equalTo(_labelZ);
    }];
    [self.buttonF mas_makeConstraints:^(MASConstraintMaker *make){
        make.right.mas_equalTo(-10);
        make.top.mas_equalTo(15);
        make.width.height.mas_equalTo(30);
    }];
    
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:20], NSFontAttributeName,nil];
    CGRect rect = [self.model.content boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    
    self.labelT.frame = CGRectMake(10, self.labelS.frame.size.height+self.labelS.frame.origin.y+10, self.contentView.frame.size.width-20, rect.size.height);

 
        
        if (_model.attachments.count == 0) {
            self.imageN.frame = CGRectMake(0, 0, 0, 0);
        }else{
            self.imageN.frame = CGRectMake(10, self.labelT.frame.size.height+10+self.labelT.frame.origin.y, self.contentView.frame.size.width-20, 250);
        }
    
    
    
    self.Ptable.frame = CGRectMake(10, self.imageN.frame.size.height+self.labelT.frame.size.height+self.labelT.frame.origin.y+20, self.contentView.frame.size.width-20, self.heightC);
    
//    
    self.Ptext.frame = CGRectMake(20, self.Ptable.frame.size.height+10+self.Ptable.frame.origin.y, self.Ptable.frame.size.width-40, 30);

}
-(void)hui{
    
    NSInteger heightZ = 10+10+10+10+10+10+self.imageT.frame.size.height+self.labelT.frame.size.height+self.imageN.frame.size.height+self.Ptable.frame.size.height+self.Ptext.frame.size.height+10;
    NSString *str = [NSString stringWithFormat:@"%ld",(long)heightZ];
    
   [[NSNotificationCenter defaultCenter] postNotificationName:@"changeHeight" object:str];
    
}
-(void)setModel:(expressBiao *)model{
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self.imageT sd_setImageWithURL:[NSURL URLWithString:_model.headimg]];
    self.labelZ.text = _model.authorname;
    self.labelS.text = _model.publishtime;
     self.labelT.text = _model.content;
    self.attch = _model.attachments.firstObject;
    [self.imageN sd_setImageWithURL:[NSURL URLWithString:_attch.picurl]];
    
    [self hui];
}



#pragma make tableView的点击方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _model.commentlist.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    expressXiaoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Pcell forIndexPath:indexPath];
    commentlist *model = [_model.commentlist objectAtIndex:indexPath.row];
    cell.model = model;
    
      return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    //计算文字高度
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil];//默认17
    //这里的宽度一定要和我们要显示文字的lable的宽度一样
    
    commentlist *model = [_model.commentlist objectAtIndex:indexPath.row];
    
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake(self.contentView.frame.size.width-20, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    if (_model.commentlist.count == 1) {
        self.heightC = 55 + rect.size.height;
    }
    if (_model.commentlist.count == 2) {
        self.heightC = 55 + rect.size.height+55+rect.size.height;
    }
    if (_model.commentlist.count == 3) {
        self.heightC = 165+rect.size.height*3;
    }
    

    return 55 + rect.size.height;
    
   
    
}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
